
import Dialog from '../../dist/dialog/dialog';
// pages/rush/rush.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rush_num:'',
    rush_question:'',
    rush_answer:'',
    rush_result:'',
    rush_guanka:'',
    disabled:'true',
    size:'default'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    
     let that=this;
     var arr=new Array()
     that.setData({
       rush_answer:arr
     })
     var result=new Array()
     that.setData({
       rush_result: result
     })

    var num = Number(options.qanumber) 
    var guankaing= Number(options.guanka)
        //   console.log("thats.data.rush_no========" + that.data.rush_no)
        //  console.log("num============" + that.data.rush_no)
       // console.log("================================" + num)
    console.log("options.guanka          "+options.guanka)
        that.setData({
          rush_num:num
        })
        that.setData({
          rush_guanka:guankaing
        })

    console.log("that.data.rush_num"+that.data.rush_num)
        wx.request({
          url: 'https://sdb.doways.cn/qa/GetQuestion2.do',
          data: {
            wx_openid: wx.getStorageSync('openid'),
            Guanka: num,
            tishu: 10
          },
          method: 'post',
          headers: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log("openid:"+wx.getStorageSync('openid'))
            console.log(num+1)
            console.log("res.data.info:"+res.data.info)
            console.log(res.data.info)
            that.setData({
              rush_question: res.data.info,
            })

            wx.setStorageSync("question", res.data.info)

          }

        })

  

  
    
   


  },
  radioChange:function(event){
    
    let that=this
    var select=that.data.rush_answer
    var arr = event.detail.value.split("-");
    select[arr[0]-1]=arr[1];
    that.setData({
      rush_answer:select,
      disabled:false
    })
    
   
  },
  submit:function(){
    
    var that=this
    var right=new Array();
    var temp = new Array();
    for(var i=0;i<that.data.rush_question.length;i++){
      right[i] = that.data.rush_question[i].answer_right
    }

    for (var j=0; j < that.data.rush_question.length;j++){
      if(right[j]!=that.data.rush_answer[j]){
        //console.error("compared:             "+right[j] + "   " + that.data.rush_answer[j])
       temp[right.length-(right.length-j)]= Number(j+1)
        that.data.rush_question[j].id=-1
      }else{
       //temp[right.length - (right.length - j)] = Number(-1);
        that.data.rush_question[j].id = 1
      }
    }
    wx.setStorageSync("question", that.data.rush_question)
    console.log(temp)
    that.setData({
      rush_result:temp
    })
    
    if (that.data.rush_result.length==0) {
      // var num = Number(wx.getStorageSync('Guanka'))
      // if(num==0){
      //   num=2
      // }else{
      //   console.log("NUM:      "+num + 1)
      //   num=num+1
      // }
      // console.log("NUM-------:      " + num )
      
      wx.vibrateShort();
      wx.vibrateShort();
      wx.vibrateShort();
      console.log("that.data.rush_guanka   "+that.data.rush_guanka)
      console.log("that.data.rush_num       " + that.data.rush_num)
      if (that.data.rush_num>that.data.rush_guanka){
        var num = Number(that.data.rush_guanka) + 1 
      Dialog.confirm({
        title: '结果',
        message: '👍恭喜全对，可以进入下一关了点击确认回到选关页进入下一关，点击取消停留在此页！'
      }).then(() => {
        wx.request({
          url: 'https://sdb.doways.cn/qa/UpdateGuanka.do',
          data: {
            wx_openid: wx.getStorageSync('openid'),
            update_guanka: num
          },
          method: 'post',
          headers: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
          
            wx.reLaunch({
              url: '/pages/list/list',
            })



          }

        })
        
      }).catch(() => {
        // on cancel
      });
    }else{
        Dialog.confirm({

          title: '结果',
          message: '👍恭喜全对，点击确认回到选关页点击取消停留在此页！'
        }).then(() => {
          wx.reLaunch({
            url: '/pages/list/list',
          })
        }).catch(() => {
          // on cancel
        });
    }
    }else{
      wx.vibrateLong();
      wx.showLoading({
        title: '加载中',
      })

      setTimeout(function () {
        wx.hideLoading()
        wx.reLaunch({
          url: '/pages/result/result',   
      })
      }, 2000)
      
      
        
      
       
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
 
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})